package com.megalith.service.impl;

import cn.hutool.core.util.StrUtil;
import com.megalith.constant.ResultCode;
import com.megalith.dto.MembershipDTO;
import com.megalith.dto.UserGroupDTO;
import com.megalith.mapper.ProcessEngineUserGroupMapper;
import com.megalith.service.ProcessEngineUserGroupService;
import com.megalith.vo.Result;
import com.megalith.vo.UserGroupVO;
import com.megalith.vo.UserGroupWithUserVO;
import lombok.extern.slf4j.Slf4j;
import org.flowable.engine.IdentityService;
import org.flowable.idm.api.Group;
import org.flowable.idm.api.User;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.List;
import java.util.UUID;
import java.util.stream.Collectors;

/**
 * @Description TODO
 * @Author lfp
 * @Date 2022/7/16
 * @Version 1.0
 */
@Service
@Slf4j
public class ProcessEngineUserGroupServiceImpl implements ProcessEngineUserGroupService {
    @Autowired
    private IdentityService identityService;

    @Autowired
    private ProcessEngineUserGroupMapper processEngineUserGroupMapper;


    @Override
    public Result<UserGroupVO> addGroup(UserGroupDTO groupDTO) {
        if(StrUtil.isNotBlank(groupDTO.getId()) && ifHaveGroup(groupDTO.getId())){
            return Result.sysError("用户组ID已存在！");
        }

        Group group = identityService.newGroup(generateGroupId(groupDTO.getId()));
        group.setName(groupDTO.getName());
        group.setType(groupDTO.getType());

        identityService.saveGroup(group);
        return null;
    }

    @Override
    public Result deleteGroup(String groupId) {
        if(!ifHaveGroup(groupId)){
            return Result.sysError("未查询到该用户组信息");
        }

        identityService.deleteGroup(groupId);//会级联删除用户关联关系
        return Result.ok(ResultCode.SYS_SUCCESS.getDes());
    }

    @Override
    public List<UserGroupWithUserVO> listUserGroupWithUser(UserGroupDTO groupDTO) {
        List<UserGroupWithUserVO> userGroupWithUserVOS = processEngineUserGroupMapper.queryUserGroupWithUsersByGroupId(groupDTO);

        List<UserGroupWithUserVO> collect = userGroupWithUserVOS.stream().map(group -> {
            group.setUserList(processEngineUserGroupMapper.selectUsersByGroupId(group.getId()));
            return group;
        }).collect(Collectors.toList());
        return collect;
    }

    @Override
    public Result createMembership(MembershipDTO membershipDTO) {
        if(!ifHaveUser(membershipDTO.getUserId())){
            return Result.sysError("用户不存在!");
        }
        if(!ifHaveGroup(membershipDTO.getGroupId())){
            return Result.sysError("用户组不存在!");
        }
        identityService.createMembership(membershipDTO.getUserId(),membershipDTO.getGroupId());
        return Result.ok(ResultCode.INSERT_SUCCESS.getDes());
    }

    /**
     *@Description 判断用户组是否存在
     *@Author lfp
     *@Date 2022/7/16
     *@param groupId
     *@Return
     */
    public boolean ifHaveGroup(String groupId){
        List<Group> list = identityService.createGroupQuery()
                .groupId(groupId)
                .list();
        return list.size()>0;
    }

    /**
     *@Description 判断用户是否存在
     *@Author lfp
     *@Date 2022/7/16
     *@param userId
     *@Return
     */
    public boolean ifHaveUser(String userId){
        List<User> list = identityService.createUserQuery()
                .userId(userId)
                .list();
        return list.size()>0;
    }

    //获取用户组id
    public String generateGroupId(String groupId){
        if(StrUtil.isBlank(groupId)){
            groupId = UUID.randomUUID().toString().replace("-","");
        }
        return groupId;
    }
}
